
var libraryOpen = false;
var library = {};
var imageInsertDialog = false;


if(! window.console)
{
    window.console =
    {
        log:function(l)
        {

        }
    }
}

function openLibrary(callback,element,directInsert){
    if (!libraryOpen){
        libraryOpen = true;
        var libraryElement = document.createElement("div");
        libraryElement.setAttribute("id", "library");
        document.body.appendChild(libraryElement);

        library = $(libraryElement);
        library.css({
            display:"none"
        });
        library.load(libraryDialogUrl);

        library.dialog({
            autoOpen: false,
            width:660,
            height:580
        });

        var libraryInsertImageElement = document.createElement("div");
        libraryInsertImageElement.setAttribute("id", "insertImageDialog");
        document.body.appendChild(libraryInsertImageElement);

        imageInsertDialog = $(libraryInsertImageElement);
        imageInsertDialog.css({
            display:"none"
        });

        imageInsertDialog.html('<div id="iWidthWrapper"><label style="width:100px;display:block;" for="iWidth">Bredde:</label><input name="iWidth"/></div>\n\
    <div id="iHeightWrapper"><label style="width:100px;display:block;" for="iHeight">Højde:</label><input name="iHeight"/></div>\n\
    <div></div>');

        $(imageInsertDialog).dialog({
            titel:"IMAGEPATH",
            autoOpen: false,
            width:220,
            height:190,
            resizable: false,
            modal: true,
            buttons: {
                'Insæt billede': function() {
                    var url_ekstra = "";
                    var w = $("input[name=iWidth]").attr("value");
                    var h = $("input[name=iHeight]").attr("value");

                    if (w != "")
                        url_ekstra += "&width="+w;
                    if (h != "")
                        url_ekstra += "&height="+h;


                    var insertUrl = encodeURI(imageInsertDialog.src+url_ekstra);

                    if (!library.insertCallback){
                        window.console.log("Default library callback called");
                        library.defaultInsertCallback(insertUrl,element);
                    }
                    else{
                        window.console.log("custom library callback called");
                        library.insertCallback(insertUrl,element);
                    }
                    $(this).dialog('close');
                    
                },
                "Anuller": function() {
                    $(this).dialog('close');
                }
            }
        });
        library.defaultInsertCallback = function(url,el){
            $(el).val(url);
        }

    }

    if (callback){
        window.console.log("Using custom library callback");
        library.insertCallback = callback;
    }else{
        window.console.log("Using default library callback");
        library.insertCallback = false;
    }
    if(directInsert)
    {
        library.directInsert = true;
    }
    else
    {
        library.directInsert = false;
    }
    library.element = element;

    library.dialog("open");
    library.dialog("option","title","Bibliotek");
//library.editor = markItUp;
}



function insertImage(image){
    if(library.directInsert)
    {
        if (!library.insertCallback){
            window.console.log("Default library callback called");
            library.defaultInsertCallback(image,library.element);
        }
        else{
            window.console.log("custom library callback called");
            library.insertCallback(image,library.element);
        }
        return;
    }

    window.console.log("Insert image dialog called");
    imageInsertDialog.src = image;
    var name;
    var aName = image.split("/");
    var name = aName[aName.length-1];
    imageInsertDialog.dialog("option","title",name);
    imageInsertDialog.dialog("open");
}
